﻿//
// (C) Copyright 2009 Irantha Suwandarathna (iranthas@hotmail.com)
// All rights reserved.
//

/* Copyright (c) 2001-2008, The HSQL Development Group
 * All rights reserved.
 *
 * Redistribution and use _in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions _in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer _in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of the HSQL Development Group nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EffiProz.Core.Results;
using EffiProz.Core.Lib;

namespace EffiProz.Core
{
    public abstract class Statement
    {
        public const int META_RESET_VIEWS = 1;
        public const int META_RESET_STATEMENTS = 2;

        //
        public static Statement[] emptyArray = new Statement[] { };

        //
        public int type;
        public int group;
        protected bool _isLogged = true;
        protected bool _isValid = true;

        /** the default schema name used to resolve names in the sql */
        public QNameManager.QName schemaName;

        /** root in PSM */
        public Routine root;

        /** parent in PSM */
        public StatementCompound parent;
        public bool _isError;
        protected bool _isTransactionStatement;
        protected bool isExplain;
        protected int metaDataImpact;

        /** SQL string for the statement */
        public String sql;

        /** id in StatementManager */
        protected long id;

        /** compileTimestamp **/
        long compileTimestamp;

        /** table names read - for concurrency control */
        public QNameManager.QName[] readTableNames = QNameManager.QName.emptyArray;

        /** table names written - for concurrency control */
        public QNameManager.QName[] writeTableNames = QNameManager.QName.emptyArray;

        public OrderedHashSet references;

        public abstract Result execute(Session session);

        public void setParameters(ExpressionColumn[] _params) { }

        public Statement(int type)
        {
            this.type = type;
        }

        public Statement(int type, int group)
        {
            this.type = type;
            this.group = group;
        }

        public bool isError()
        {
            return _isError;
        }

        public bool isTransactionStatement()
        {
            return _isTransactionStatement;
        }

        public virtual bool isAutoCommitStatement()
        {
            return false;
        }

        public void setCompileTimestamp(long ts)
        {
            compileTimestamp = ts;
        }

        public long getCompileTimestamp()
        {
            return compileTimestamp;
        }

        public void setSQL(String sql)
        {
            this.sql = sql;
        }

        public virtual String getSQL()
        {
            return sql;
        }

        public virtual OrderedHashSet getReferences()
        {
            return references;
        }

        public void setDescribe()
        {
            isExplain = true;
        }

        public abstract String describe(Session session);

        public virtual QNameManager.QName getSchemalName()
        {
            return schemaName;
        }

        public void setSchemaQName(QNameManager.QName name)
        {
            schemaName = name;
        }

        public void setID(long csid)
        {
            id = csid;
        }

        public long getID()
        {
            return id;
        }

        public virtual int getType()
        {
            return type;
        }

        public int getGroup()
        {
            return group;
        }

        public bool isValid()
        {
            return _isValid;
        }

        public bool isLogged()
        {
            return _isLogged;
        }

        public virtual void clearVariables() { }

        public virtual void resolve() { }

        public virtual RangeVariable[] getRangeVariables()
        {
            return RangeVariable.emptyArray;
        }

        public QNameManager.QName[] getTableNamesForRead()
        {
            return readTableNames;
        }

        public QNameManager.QName[] getTableNamesForWrite()
        {
            return writeTableNames;
        }

        public void setParent(StatementCompound statement)
        {
            this.parent = statement;
        }

        public virtual void setRoot(Routine root)
        {
            this.root = root;
        }

        public virtual bool hasGeneratedColumns()
        {
            return false;
        }

        public virtual ResultMetaData generatedResultMetaData()
        {
            return null;
        }

        public virtual void setGeneratedColumnInfo(int mode, ResultMetaData meta) { }

        public virtual ResultMetaData getResultMetaData()
        {
            return ResultMetaData.emptyResultMetaData;
        }

        public virtual ResultMetaData getParametersMetaData()
        {
            return ResultMetaData.emptyParamMetaData;
        }
    }
}
